package com.changfeng.controller;

import com.changfeng.auth.dto.UserAuth;
import com.changfeng.bean.RetailersUser;
import com.changfeng.bean.dto.vo.*;
import com.changfeng.common.CurrentUser;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUserTopper")
    public UserTopperInfoVO getUserTopper() {
        UserAuth userAuth = CurrentUser.getCurrentUser();
        RetailersUser retailersUser = userService.getUserByKey(userAuth.getUserId());
        UserTopperInfoVO userTopperInfoVO = new UserTopperInfoVO();
        BeanUtils.copyProperties(retailersUser,userTopperInfoVO);
        return userTopperInfoVO;
    }

    @PostMapping("/signUser")
    public void signUser(@RequestBody UserSignVO userSignVO) {
        RetailersUser retailersUser = new RetailersUser();
        if (userSignVO.getPassword() == null || userSignVO.getPassword().equals("")) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        if (userSignVO.getUserName() == null || userSignVO.getUserName().equals("")) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        if (userSignVO.getPhone() == null || userSignVO.getPhone().equals("")) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }

        retailersUser.setUserName(userSignVO.getUserName());
        retailersUser.setUserPassword(userSignVO.getPassword());
        retailersUser.setUserPhone(userSignVO.getPhone());
        retailersUser.setUserNickname(userSignVO.getUserName());
        userService.signUser(retailersUser);
    }

    @PostMapping("/userAccountUpdate")
    public void userAccountUpdate(@RequestBody UserAccountUpdateVO userAccountUpdateVO) {

        userService.updateUserByAccount(userAccountUpdateVO);
    }

    @GetMapping("/getUserAccount")
    public UserAccountUpdateVO getUserAccount() {
        UserAuth userAuth = CurrentUser.getCurrentUser();
        RetailersUser retailersUser = userService.getUserByKey(userAuth.getUserId());
        if (retailersUser == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }
        UserAccountUpdateVO userAccountUpdateVO = new UserAccountUpdateVO();
        userAccountUpdateVO.setNickName(retailersUser.getUserNickname());
        userAccountUpdateVO.setPhone(retailersUser.getUserPhone());
        userAccountUpdateVO.setDate(retailersUser.getUserBirthday());
        userAccountUpdateVO.setSex(retailersUser.getUserSex());
        return userAccountUpdateVO;
    }

    @GetMapping("/getUserEmail")
    public UserEmailModifyVO getUserEmail() {
        UserAuth userAuth = CurrentUser.getCurrentUser();
        RetailersUser retailersUser = userService.getUserByKey(userAuth.getUserId());
        if (retailersUser == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }
        UserEmailModifyVO emailModifyVO = new UserEmailModifyVO();
        emailModifyVO.setEmail(retailersUser.getUserEmail());

        return emailModifyVO;
    }

    @PostMapping("/userPasswordModify")
    public void userPasswordModify(@RequestBody UserPasswordModifyVO userPasswordModifyVO){
        if (userPasswordModifyVO.getPassword() == null || userPasswordModifyVO.getPassword().equals("")) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        if (userPasswordModifyVO.getUsedPassword() == null || userPasswordModifyVO.getUsedPassword().equals("")) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }

        userService.userPasswordModify(userPasswordModifyVO);


    }

    @PostMapping("/userEmailModify")
    public void userEmailModify(@RequestBody UserEmailModifyVO userEmailModifyVO) {
        if (userEmailModifyVO.getEmail() == null || userEmailModifyVO.getEmail().equals("")) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        userService.userEmailModify(userEmailModifyVO);

    }

    @GetMapping("/getUserHead")
    public UserHeadModifyVO getUserHead() {
        UserAuth userAuth = CurrentUser.getCurrentUser();
        RetailersUser retailersUser = userService.getUserByKey(userAuth.getUserId());
        if (retailersUser == null) {
            throw new ChangFengException(ChangFengExceptionEnum.NOT_USER);
        }
        UserHeadModifyVO headModifyVO = new UserHeadModifyVO();
        headModifyVO.setImageUrl(retailersUser.getUserHeadurl());

        return headModifyVO;
    }

    @PostMapping("/userHeadModify")
    public void userHeadModify(@RequestBody UserHeadModifyVO userHeadModifyVO) {
        if (userHeadModifyVO.getImageUrl() == null || userHeadModifyVO.getImageUrl().equals("")) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        userService.userHeadModify(userHeadModifyVO);

    }

}
